package com.example.springboot.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

/**
 * @auther: Wyz
 * @date: 2023/5/6 18:31
 * @return
 */
@Slf4j
public class ReadPdf {

    public static void main(String[] args) {
//要读取的pdf文档位置
        String path = "D:/zhuomian/1.pdf";
        File file = new File(path);
        try {
            //XSSFWorkbook workbook = new XSSFWorkbook();
            //XSSFSheet sheet =workbook.createSheet();
            //加载pdf文件，创建PDDocument对象
            PDDocument document = PDDocument.load(file);
            //创建pdf文本获取对象PDFTextStripper
            PDFTextStripper pdfStripper = new PDFTextStripper();
            //获取pdf中所有信息，text中包含的就是当前pdf文档中所有信息
            String text = pdfStripper.getText(document);
            System.err.println(text);
            //根据自己的业务需求处理text中的信息
            //String data =text.replaceAll("([\\u4e00-\\u9fa5]|[\\uff0c]|[\\u3001]|[\\u3002])\\s+([\\u4e00-\\u9fa5])","$1$2");
            //String [] str=data.split("\\n");
            /**for(int i=0;i< str.length;i++){
             XSSFRow row=sheet.createRow(i);
             String string1=str[i].replaceAll(" ","");
             String[] str2=string1.split(" ");
             if(str2.length>1){
             for(int j=0;j<str2.length;j++){
             if(str2[j].equals(null)||str2[j].equals("")){
             continue;
             }
             XSSFCell cell=row.createCell(j);
             String data1=str2[j].replaceAll("\\r\\n|\\r|\\n","");
             cell.setCellValue(data1);
             }
             }else {
             XSSFCell cell=row.createCell(0);
             cell.setCellValue(string1);
             }
             }
             FileOutputStream fileOutputStream=new FileOutputStream("C:/Users/Administrator/Desktop/ccs.xlsx");
             workbook.write(fileOutputStream);
             fileOutputStream.close();*/
        } catch (Exception e) {
            System.err.println("出错");
        }
    }


    public static String getPdfContent(String url){
        String[] split = url.split("filename=");
        String substring = split[0].substring(0, split[0].length() - 1);
        File file = getFile(substring);
        PDDocument document = null;
        try {

            //加载pdf文件，创建PDDocument对象
            document =  PDDocument.load(file);

            return "ojbk";

        } catch (Exception e) {
            System.err.println("出错");
            return "出错";
        }finally {

            try {
                if (document!=null) {
                    document.close();
                    boolean delete = file.delete();
                    System.err.println(delete);
                }
            } catch (IOException e) {
                return "出错";
            }
        }
    }


    /**
     * 创建一个临时文件
     * @param url 远端文件Url
     * @return File
     */
    public static File getFile(String url) {
        //对本地文件命名
        String fileName = url.substring(url.lastIndexOf("."), url.length());
        File file = null;

        URL urlfile;
        try {
            // 创建一个临时路径 改为自己的地址方便删除
            file = File.createTempFile("file", fileName,new File("D://zhuomian\\pdf"));
            log.info("tempFile:->{}",file.getAbsolutePath());
            //下载
            urlfile = new URL(url);

            try (InputStream inStream = urlfile.openStream();
                 OutputStream os =  new FileOutputStream(file);) {
                int bytesRead = 0;
                byte[] buffer = new byte[8192];
                while ((bytesRead = inStream.read(buffer, 0, 8192)) != -1) {
                    os.write(buffer, 0, bytesRead);
                }
            }
        } catch (Exception e) {
            log.error("创建临时文件出错：-》{}",e.getMessage());
        }
        return file;
    }


}
